# 自定义过滤类
from rest_framework.filters import BaseFilterBackend


class MyFilter(BaseFilterBackend):

    def filter_queryset(self, request, queryset, view):
        # 在这里写过滤规则，假设查询规则是url后面拼接?teacher='老师姓'
        params = request.GET.get('teacher')  # 先从请求中取出teacher=后面的老师姓
        queryset = queryset.filter(老师名字=params)  # 按照传来的老师名字查出数据
        return queryset[:1]  # 不论查出来的有多少，只返回一条


from django_filters.filterset import FilterSet
from . import models
from django_filters import filters


class CourseFilterSet(FilterSet):
    # 课程的价格范围要大于min_price，小于max_price
    min_price = filters.NumberFilter(field_name='price', lookup_expr='gt')  # gt 大于  gte 大于等于
    max_price = filters.NumberFilter(field_name='price', lookup_expr='lt')  # lt 小于  lte 小于等于

    class Meta:
        model = models.Course
        fields = ['course_category']
